<template>
  <div class="login-container">
    <div class="userForm">
      <div class="form-title">
        <h2>{{ $t("login.title") }}</h2>
      </div>
      <div class="user-item">
        <input
          type="text"
          v-model="loginData.username"
          required />
        <label>
          <svg-icon
            icon-class="user"
            size="1.2em" />Username</label
        >
      </div>
      <div class="user-item">
        <input
          v-model="loginData.password"
          :type="passwordVisible === false ? 'password' : 'text'"
          required />
        <label>
          <svg-icon icon-class="password" />
          Password
        </label>
      </div>
      <a
        @click="login">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        Submit
      </a>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import {
  LocationQuery,
  LocationQueryValue,
  useRoute,
} from "vue-router";
import router from "@/router";
import SvgIcon from "@/components/SvgIcon/index.vue";


const route = useRoute();
const loginData = reactive({
  username: "",
  password: "",
});

/**
 * 密码是否可见
 */
const passwordVisible = ref(false);

function login() {
  if (loginData.username == "" || loginData.password == "") {
    ElMessage({
      message: "请输入用户名和密码",
      type: "warning",
    });
    return;
  }
  if (
    loginData.username == "admin" &&
    loginData.password == "123456"
  ) {
    const query: LocationQuery = route.query;
    const redirect = (query.redirect as LocationQueryValue) ?? "/";
    const otherQueryParams = Object.keys(query).reduce(
      (acc: any, cur: string) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      },
      {}
    );
    localStorage.setItem("accessToken",'userLogin');
    router.push({ path: redirect, query: otherQueryParams });

    loginData.username = "";
    loginData.password = "";
  }
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  background: linear-gradient(#141e30, #243b55);
  overflow: hidden;
}
.userForm {
  width: 520px;
  max-width: 90%;
  padding: 30px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  background-color: rgba(0, 0, 0, 0.5);
  box-shadow: 0 15px 25px rgba(0, 0, 0, 0.6);

  .form-title {
    color: #fff;
    text-align: center;
    h2 {
      font-size: 1.5rem;
      font-weight: 200;
    }
  }

  .user-item {
    position: relative;
    margin-bottom: 30px;
  }

  .user-item input {
    width: 100%;
    height: 40px;
    background: transparent;
    border: none;
    border-bottom: 1px solid #fff;
    outline: none;
    color: #fff;
    font-size: 1rem;
  }

  .user-item label {
    position: absolute;
    bottom: 0;
    left: 0;
    font-size: 1.2em;
    color: #fff;
    padding: 10px 0;
    pointer-events: none;
    transition: 0.5s;
  }

  .user-item input:focus ~ label,
  .user-item input:valid ~ label {
    bottom: 20px;
    color: #03e9f4;
    font-size: 1em;
    svg {
      font-size: 1em;
    }
  }

  a {
    position: relative;
    display: inline-block;
    padding: 10px 20px;
    color: #03e9f4;
    font-size: 1.2em;
    text-decoration: none;
    text-transform: uppercase;
    overflow: hidden;
    transition: 0.5s;
    margin-top: 40px;
    letter-spacing: 4px;
    &:hover {
      background: #03e9f4;
      color: #fff;
      border-radius: 5px;
      box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4,
        0 0 100px #03e9f4;
    }

    span {
      position: absolute;
      display: block;

      &:nth-child(1) {
        top: 0;
        left: -100%;
        width: 100%;
        height: 2px;
        background: linear-gradient(90deg, transparent, #03e9f4);
        animation: btn-anim1 1s linear infinite;
      }

      &:nth-child(2) {
        top: -100%;
        right: 0;
        width: 2px;
        height: 100%;
        background: linear-gradient(180deg, transparent, #03e9f4);
        animation: btn-anim2 1s linear infinite;
        animation-delay: 0.25s;
      }

      &:nth-child(3) {
        bottom: 0;
        right: -100%;
        width: 100%;
        height: 2px;
        background: linear-gradient(270deg, transparent, #03e9f4);
        animation: btn-anim3 1s linear infinite;
        animation-delay: 0.5s;
      }

      &:nth-child(4) {
        bottom: -100%;
        left: 0;
        height: 100%;
        width: 2px;
        background: linear-gradient(360deg, transparent, #03e9f4);
        animation: btn-anim4 1s linear infinite;
        animation-delay: 0.75s;
      }
    }
  }

  @keyframes btn-anim1 {
    0% {
      left: -100%;
    }
    50%,
    100% {
      left: 100%;
    }
  }
  @keyframes btn-anim2 {
    0% {
      top: -100%;
    }
    50%,
    100% {
      top: 100%;
    }
  }
  @keyframes btn-anim3 {
    0% {
      right: -100%;
    }
    50%,
    100% {
      right: 100%;
    }
  }
  @keyframes btn-anim4 {
    0% {
      bottom: -100%;
    }
    50%,
    100% {
      bottom: 100%;
    }
  }
}
</style>
